package peace.design.command
{
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;

import peace.util.Logger;
/**
 *  
 */
[Event (name="error", type="flash.events.ErrorEvent")] 
/**
 * Транспортная команда по http  
 */
public class NetCommand extends Command
{
	/**
	 * 
	 */
	public var request:URLRequest;
	/**
	 * 
	 */
	protected var loader:URLLoader = new URLLoader();
	/**
	 * 
	 * @param r URLRequest
	 * 
	 */	
	public function NetCommand (r:URLRequest = null)
	{
		loader.addEventListener(Event.COMPLETE, onResult, false,0,true);
		loader.addEventListener(IOErrorEvent.IO_ERROR, onFault, false,0,true);
		loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onFault, false,0,true);
		
		request = r;
	}
	
	override public function execute():void
	{
		super.execute();
		if (request) loader.load(request);
	}
	/**
	 * virtual
	 * 
	 * @param e
	 * 
	 */	
	protected function onResult (e:Event):void
	{
		notifyComplete();
	}
	/**
	 * virtual
	 * 
	 * @param e
	 * 
	 */	
	protected function onFault (e:ErrorEvent):void
	{
		Logger.error (e.text);
		
		dispatchEvent (e.clone());
		
		notifyComplete();
	}
	
}
}